home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_12
/
guthrie2
/
xlate.c
< prev
Wrap
C/C++ Source or Header
|
1994-09-25
|
20KB
|
488 lines
/**************************************************************************
* File Name : XLATE.C
* Description : Text Substitution Software tool for Multi Language
* Support.
* Author : R. Scott Guthrie / All Rights Reserved
* History : 01/13/94 - Modification to add Speed-Up code for
* loading '.TRB' files.
* 01/20/94 - Function names changed to avoid possible
* conflicts with users code.
**************************************************************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Function : XlateSet()
* Arguments : (char *) File name of translation file (without ext).
* Return : None
* Description : USER CALLABLE - This function sets the file name
* to be used for string translation.
* If user does not call this function, the translate
* system will use the default file specified in
* constant 'DefaultFileName'. No checking for
* file existance is performed by this function.
* ---------------
* Function : Xlate()
* Arguments : (char *) String to be translated.
* Return : (char *) Translation string.
* Description : USER CALLABLE - This function translates the string
* passed in, to the translated version in the translate
* table. If the table has not been loaded, it
* initiates that action before attempting the
* translation. If no match is found, then a '?' is
* returned.
* ---------------
* Function : XlateLoad()
* Arguments : (char *) File name of translation file.
* Return : (int) function status
* Description : INTERNAL FUNCTION - Called by 'Xlate()' to build the
* translation table.
* ---------------
* Function : XlateLoadBinary()
* Arguments : (FILE) File Descriptor of the binary translation file.
* Return : (int) function status
* Description : INTERNAL FUNCTION - Called by 'XlateLoad()' to
* load the binary translation table from a TRB file.
* ---------------
* Function : XlateLoadText()
* Arguments : (FILE) File descriptor of the Text translation file.
* Return : (int) function status
* Description : INTERNAL FUNCTION - Called by 'XlateLoad()' to
* build and load the translation table from a TRN file.
* ---------------
* Function : XlateFree()
* Arguments : None
* Return : None
* Description : USER CALLABLE - This function is used to free the
* memory allocated for the TRANSLATE system. It is used
* internally and callable directly.
* ---------------
* Function : XlateSearchCompare()
* Arguments : (const void *) Pointer to compare string 1.
* (const void *) Pointer to compare string 2.
* Return : (int) Compare status
* Description : INTERNAL FUNCTION - Search Compare Function.
* ---------------
* Function : XlateSortCompare()
* Arguments : (const void *) Pointer to compare string 1.
* (const void *) Pointer to compare string 2.
* Return : (int) Compare status
* Description : INTERNAL FUNCTION - Sort Compare Function.
* ---------------
* Function : XlateGetString()
* Arguments : (FILE) File descriptor of the Text translation file.
* (char *) Pointer to a string.
* Return : (int) Number of characters in string or -1 for EOF
* Description : INTERNAL FUNCTION - Called by 'XlateLoadText()'
* Reads and parses through the TRN file and returns
* the next String value found.
* ---------------
* Function : XlateCreateTable()
* Arguments : (int) Number of XLATE entries.
* Return : (int) 1 for success, -1 for Allocation Error.
* Description : INTERNAL FUNCTION - Called by 'XlateLoadText()'
* and 'XlateLoadBinary()' to allocate memory
* for the Translate Table.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <alloc.h>
#include <assert.h>
#include "xlate.h"
/* GLOBAL VARAIBLES */
XLATE *XlateBase = NULL;
int XlateLines = 0;
char *XlateFile = XlateDefaultFileName;
/* FUNCTION DEFINITIONS */
/*========================================*/
void XlateSet(char *translate_file_name)
/*========================================*/
/* Establish Translation Table Name */
{
XlateFree(); /* clear any existing table */
XlateFile = translate_file_name; /* set to new file name. */
return;
} /* end function 'XlateSet()' */
/*=============================*/
char *Xlate(char *key_string)
/*=============================*/
/* Translate a given KEY string to a Translated version by searching */
/* through the Translation table. */
{
XLATE *node; /* Allocate pointer to an XLATE node */
static char Default[] = "?"; /* Default String */
/* If the translation file has not been read in, then load it. */
if(!XlateBase)
(void) XlateLoad(XlateFile);
if(!XlateBase) /* If file wasn't loaded (found), */
return(Default); /* Return the default string */
/* Search translation block for matching key string. */
if((node = (XLATE *)bsearch(key_string, XlateBase,
XlateLines, sizeof(XLATE), XlateSearchCompare)) == NULL)
return(Default); /* If no KEY is found */
/* Return pointer to the translated value. */
return(node->translated_value);
} /* end function 'Xlate()' */
/*=============================*/
int XlateLoad(char *filename)
/*=============================*/
/* This function is used to load a translation definition file. */
/* Returns the number of Translate Table Entires or: */
/* 0 if no file is found. (either ".TRB" or ".TRN") */
/* -1 if memory allocation error encountered. */
{
char filenamebuf[128]; /* Filename Buffer */
FILE *fd; /* Translation File Descriptor */
int retval = 0; /* return variable */
/* Delete any existing 'in memory' translate table */
XlateFree();
/* Attempt to open a Binary Version of the translate file. */
sprintf(filenamebuf, "%s.TRB", filename);
if((fd = fopen(filenamebuf, "rb")) != NULL)
{
retval = XlateLoadBinary(fd);
fclose(fd); /* Finished with file */
}
else /* Binary file did not open */
{
/* Attempt to open a Text Version of the translate file. */
sprintf(filenamebuf, "%s.TRN", filename);
if((fd = fopen(filenamebuf, "r")) != NULL)
{
retval = XlateLoadText(fd);
fclose(fd); /* Finished with file */
}
}
/* If there was an error, then free any memory that did happen */
/* to get allocated for the table and any Keys or Results. */
if(retval <= 0)
XlateFree();
return(retval);
} /* end 'XlateLoad()' */
/*=============================*/
int XlateLoadBinary(FILE *fd)
/*=============================*/
/* Loads the binary version of the translate file. This loads */
/* faster than the Text version. */
{
/* Binary File format is: */
/* 4 bytes = ".TRB" (File Signature) */
/* integer # of lines */
/* integer # of line sets of: */
/* integer size of key string */
/* null terminated key string */
/* integer size of result string */
/* null